const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectId;
// const url = 'mongodb://taosha_user:apyl8XWP6LSW4JHs@150.158.169.16:27777/taosha';
const url = 'mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha';

async function main () {
  const client = await MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true });
  const db = client.db('taosha');

  let list = await db.collection('messages')
    .find({})
    .skip(0)
    .limit(10000)
    .sort({ _id: -1 })
    .toArray();

  for (let i = 0; i < list.length; i++) {
    let item = list[i];
    let result = await db.collection('messages').updateOne({ _id: item._id }, {
      $set: {
        reply_id: item.reply_id?.toString(),
        topic_id: item.topic_id?.toString(),
        author_id: item.author_id?.toString(),
        master_id: item.master_id?.toString()
      }
    });
    console.log(i, list.length);
  }

  client.close();
}

main();
